package com.primaryedu.adapter;

import java.util.ArrayList;
import java.util.List;

import com.primaryedu.R;
import com.primaryedu.bean.NoticeBean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 通知适配器
 *
 * @author jun.wang
 * @description
 * @data 2016年12月15日
 */
public class InformAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
    private List<NoticeBean> lists = new ArrayList<>();

    public InformAdapter(Context context, List<NoticeBean> lists) {
        this.context = context;
        this.lists = lists;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void updateView(List<NoticeBean> nowList) {
        this.lists = nowList;
        this.notifyDataSetChanged();//强制动态刷新数据进而调用getView方法
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_inform, null);
            viewHolder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            viewHolder.text_content = (TextView) convertView.findViewById(R.id.text_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        NoticeBean bean = lists.get(position);
        viewHolder.text_date.setText(bean.getReleaseTime());
        viewHolder.text_content.setText(bean.getTitle());

        // 是否已读
        if (bean.getIfread().equals("1")) {//1:已读 2：未读
            viewHolder.text_content.setTextColor(context.getResources().getColor(R.color.text_default_two));
            viewHolder.text_content.setBackgroundResource(R.drawable.default_cornor);
        } else {
            viewHolder.text_content.setTextColor(context.getResources().getColor(R.color.black));
            viewHolder.text_content.setBackgroundResource(R.drawable.white_cornor);
        }

        return convertView;
    }

    class ViewHolder {
        TextView text_date;
        TextView text_content;

    }
}
